package com.cqgc.service.impl;

import com.cqgc.exception.AddDeliveryException;
import com.cqgc.exception.SelectDeliveryException;
import com.cqgc.exception.UpdateDeliveryException;
import com.cqgc.mapper.DeliveryMapper;
import com.cqgc.mapper.GoodsMapper;
import com.cqgc.mapper.OrderMapper;
import com.cqgc.mapper.UserMapper;
import com.cqgc.pojo.dto.DeliveryDTO.*;
import com.cqgc.pojo.dto.UserDTO.UserRegisterDTO;
import com.cqgc.pojo.entity.Delivery;
import com.cqgc.pojo.entity.Order;
import com.cqgc.pojo.entity.PageResult;
import com.cqgc.pojo.entity.User;
import com.cqgc.service.DeliveryService;
import com.cqgc.utils.ThreadLocalUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author : 是你
 * @time : 2024/4/24 16:28
 */
@Service
public class DeliveryServiceImpl implements DeliveryService {

    @Autowired
    private DeliveryMapper deliveryMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public int add(AddDeliveryDTO addDeliveryDTO) {
        Delivery delivery = new Delivery();

        // 根据订单编号查找订单信息
        Order order = orderMapper.findOrderByOid(addDeliveryDTO.getOrderNumber());
        // 根据用户ID查找用户信息
        User user = userMapper.findByUser(addDeliveryDTO.getUid());

        Map<String, Object> map = ThreadLocalUtil.get();

        // 设置配送信息
        delivery.setOrderNumber(order.getOrderNumber());
        delivery.setDeliveryPhone(user.getPhone());
        delivery.setDeliveryAccount(user.getAccount());
        delivery.setCreateUser((String) map.get("account"));
        delivery.setUpdateUser((String) map.get("account"));
        delivery.setCreateTime(LocalDateTime.now());
        delivery.setUpdateTime(LocalDateTime.now());
        delivery.setOrderAddress(order.getAddress());

        // 检查该用户今日派单是否已达上限
        int count = deliveryMapper.count(user.getAccount());
        if (count >= 5) {
             throw new AddDeliveryException("今日该用户派单上限");
        }
        return deliveryMapper.add(delivery);
    }


    @Override
    public List<Delivery> list() {
        Map<String,Object> map = ThreadLocalUtil.get();
        String deliveryAccount = (String) map.get("account");
        Integer uid = (Integer) map.get("uid");
        int role = goodsMapper.findRoleByUid(uid);
        if (role == 2 || role == 1){
            //提供商和管理员
            return deliveryMapper.listAll();
        } else if (role == 4) {
            //配送员
            return deliveryMapper.list(deliveryAccount);
        }
        throw  new SelectDeliveryException("您无权查看");
    }

    /**
     * 更新配送信息
     * @param delivery 配送实体，包含需要更新的配送信息
     * @return 更新成功返回1，更新失败返回0
     */
    @Override
    @Transactional
    public int update(Delivery delivery) {
        // 根据配送账号查找用户
        User user = userMapper.findAccount(delivery.getDeliveryAccount());
        // 验证用户手机号与配送手机号是否一致
        if (user.getPhone().equals(delivery.getDeliveryPhone())){
            // 一致则更新配送信息
            return deliveryMapper.update(delivery);
        }
        // 不一致则不更新，返回0
        throw new UpdateDeliveryException("账号和手机号必须一致");
    }

    @Override
    @Transactional
    public int delete(DeleteDeliveryDTO id) {
        return deliveryMapper.delete(id);
    }

    @Override
    @Transactional
    public int updateStatus(UpdateDeliveryStatusDTO updateDeliveryStatusDTO) {
        Delivery delivery = deliveryMapper.findById(updateDeliveryStatusDTO.getId());
        Order order = new Order();
        order.setOrderNumber(delivery.getOrderNumber());
        order.setStatus(4);
        orderMapper.updateStatus(order);
        return deliveryMapper.updateStatus(updateDeliveryStatusDTO);
    }

    @Override
    public PageResult page(DeliveryPageDTO deliveryPageDTO) {

        Map<String,Object> map = ThreadLocalUtil.get();
        String deliveryAccount = (String) map.get("account");
        Integer uid = (Integer) map.get("uid");
        int role = goodsMapper.findRoleByUid(uid);
        if (role == 2 || role == 1){
            //提供商和管理员
            PageHelper.startPage(deliveryPageDTO.getCurrentPage(),deliveryPageDTO.getPageSize());
            Page<Delivery> page = deliveryMapper.pageAll(deliveryPageDTO);
            return new PageResult(page.getTotal(),page.getResult());
        } else if (role == 4) {
            //配送员
            PageHelper.startPage(deliveryPageDTO.getCurrentPage(),deliveryPageDTO.getPageSize());
            Page<Delivery> page = deliveryMapper.page(deliveryPageDTO,deliveryAccount);
//            return deliveryMapper.list(deliveryAccount);
            return new PageResult(page.getTotal(),page.getResult());
        }
        throw  new SelectDeliveryException("您无权查看");
//        Page<Delivery> page = deliveryMapper.page(deliveryPageDTO);
//        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public PageResult like(DeliveryLikeDTO deliveryLikeDTO) {
        PageHelper.startPage(deliveryLikeDTO.getPageNum(),deliveryLikeDTO.getPageSize());
        Page<Delivery> page = deliveryMapper.like(deliveryLikeDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }
}
